home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / srt.c < prev    next >
C/C++ Source or Header  |  1985-06-03  |  4KB  |  88 lines

  1. #include          <stdio.h>
  2. #include          <ctype.h>
  3.  
  4. #define  MAXWORD  80                           /* maximum word length */
  5. #define  NEWLINE  putc('\n',fp)     /* put carraige return in file fp */
  6.  
  7. struct text {               /* global declaration of tree node 'text' */
  8.              char word[MAXWORD];
  9.              struct text *lptr;
  10.              struct text *rptr;
  11. };
  12.  
  13. struct text *root;         /* global declaration of root of text tree */
  14.  
  15. main(argc, argv)
  16.   /*-----------------------------------------------------------------*/
  17.   /* driver for sort procedure.  Srt will sort lines of a specified  */
  18.   /* input file and place the result in either the stdout or a       */
  19.   /* specified output file.  Program is written for Lattice C but    */
  20.   /* functions provided in its library are documented so that srt    */
  21.   /* might be modified to suit your compiler.  If you have any       */
  22.   /* trouble or questions give me a call (if you can find me)        */
  23.   /*-----------------------------------------------------------------*/
  24.   /* Joe R Wyatt    2311 Lubbock, Tx  79412                          */
  25.   /* Home #: 806/793-5689    Office #: 806/763-8011 ext: 239         */
  26.   /*-----------------------------------------------------------------*/
  27.   /* Variables :        fp --     pointer to input file              */
  28.   /*                   fp2 --     ptr to output file                 */
  29.   /*                     c --     integer returned from getw.        */
  30.   /*                     i --     processed line count               */
  31.   /*                     j --     empty line count                   */
  32.   /*                  word --     temporary input line storage       */
  33.   /* Procedures:      getw --     included                           */
  34.   /*                 putwt --     included                           */
  35.   /*                 ptree --     included                           */
  36.   /* Functions :    printf --     string compare                     */
  37.   /*                 fopen --     open file and return file pointer  */
  38.   /*                  exit --     premature exit from program        */
  39.   /*                fclose --     close file                         */
  40.   /*-----------------------------------------------------------------*/
  41. int argc;
  42. char *argv[];
  43. {
  44.      FILE *fp, *fp2, *fopen();
  45.      int c, i, j;
  46.      char word[MAXWORD];
  47.  
  48.      root = NULL;                                /* root of text tree */
  49.      if (argc == 1)  {                  /* no file specified for sort */
  50.           printf("usage: srt infile.ext <outfile.exe>\n");
  51.           exit(0);
  52.      }
  53.      else{                                         /* open input file */
  54.           if ((fp = fopen(*(argv+1), "r")) == NULL) {
  55.                printf("cannot read %s\n",*(argv+1));
  56.                exit(1);
  57.           }
  58.           if (argc == 3) {                        /* open output file */
  59.                if ((fp2 = fopen(*(argv+2), "w")) == NULL) {
  60.                     printf("cannot open %s \n",*argv[3]);
  61.                     exit(1);
  62.                }
  63.           }
  64.           else
  65.                fp2 = stdout;                   /* no output specified */
  66.      }
  67.      i = 0;
  68.      j = 0;
  69.      while((c = getw(fp,word)) != EOF) {     /* get next line of file */
  70.           if (strlen(word) != 0) {
  71.                putwt(word);           /* if not null then put in tree */
  72.                i++;            /* increment number of lines processed */
  73.           }
  74.           else
  75.                j++;                 /* else inc number of empty lines */
  76.      }
  77.      fclose(fp);
  78.      ptree(fp2, root);                           /* print sorted tree */
  79.      printf("\nprocedure concluded.\n%d lines processed\n",i);
  80.      printf("%d empty lines ignored.\n",j);
  81.      fclose(fp2);
  82. }
  83.  
  84. putwt(w)
  85.   /*-----------------------------------------------------------------*/
  86.   /* procedure places 'w' in tree named 'text'.                      */
  87.   /* Parameters:         w --     null ended string                  */
  88.   /* Variables :